require(['validator', 'zTreeCheckbox'], function () {
    var userInfo = commonFun.execParentMethod("getUserInfo");               //当前登录用户信息
    var $right = $("#right"),
        $sysCorpTree = $("#sysCorpTree"),
        selCorpId = userInfo.corpId,
        corpName = "",
        corpId = "no";
    var $left = $('#left');
    var $maintenanceItemForm = $('#maintenanceItemForm');
    $maintenanceItemForm.initForm();

    $("#corpId").val(selCorpId);

    function initsysCorpTree(nodeId){
        var node;
        $.ajaxExt({
            url : "/sysCorpInfo/getJurisdictionCorpInfoList.htm"
        }).done(function(data){
            if(commonFun.execParentMethod('getUserInfo').roleType ==='ROLT0001'){
                if (!data) {
                    data = [];
                }
                // 树的参数配置
                var setting = {
                    data: {
                        key: {
                            name: "corpName"
                        },
                        simpleData: {
                            enable: true,
                            idKey: "corpId",
                            pIdKey: "parentCorpId",
                            rootPId: 0
                        }
                    },
                    view: {
                        showIcon: true
                    },
                    callback: {
                        onClick: sysCorpTreeClick
                    }
                };
                //生成树
                sysCorpTreeObj = $sysCorpTree.createTree(setting, data, {search: $('#searchlabel')});//$.fn.zTree.init($sysCorpTree, setting, data);
                sysCorpTreeObj.expandAll(true);
                if(typeof nodeId !== "undefined"){//选中对应节点
                    sysCorpTreeObj.setting.view.expandSpeed = "";
                    sysCorpTreeObj.setting.view.expandSpeed = "fast";
                    node = sysCorpTreeObj.getNodesByFilter(function(node) { return node.corpId == nodeId; }, true);
                }else{//选中第一个节点
                    node = sysCorpTreeObj.getNodesByFilter(function(node) { return node.level == 0; }, true);
                }
                sysCorpTreeObj.selectNode(node,false);
                sysCorpTreeClick(null,node.corpId,node,true);
            }else{
                $("#left").hide();
                $("#corpCd").attr("readonly", true);
                $("#enabledSt0").attr("disabled", true);
                $("#enabledSt1").attr("disabled", true);
                $("#corpType").attr("disabled", true);
                initRight();
            }
        });
    }

    function initHeight(){
        var height = $(window).height();
        $sysCorpTree.parent().height(height - 100);
        $sysCorpTree.height(height - 105);
        $('#left').height(height);
        $('#right').height(height - 50);
    }

    /**
     * 点击树节点
     */
    function sysCorpTreeClick(event, treeId, treeNode,clickFlag){
        var htm = '<tbody><tr id="r-th">';
        htm += '<th width="40%" style="border-left: 1px solid #DCDCDC">维保项目</th>\
            <th width="40%">备注</th>\
            <th>操作</th>\
            </tr>\
            <tr>\
            <td style="border-left: 1px solid #DCDCDC" class="add-line" colspan="5">\
            <a class="tjhang" href="javascript:;" id="addRowA"><em>+</em>添加行</a>\
            </td>\
            </tr>\
            </tbody>'


        $(".table-style1").html(htm);
        selCorpId = treeNode.corpId;
        corpName = treeNode.corpName;
        $("#corpId").val(selCorpId);
        initRight();
    }

    $(window).on('resize', function () {
        $left.height($(window).height() - 67);
    }).trigger('resize');

    // 初始化右侧
    var initRight = function () {
        $.ajaxExt({
            async: false,
            url: "/funMaintenaSet/getMaintenaItemInfo.htm",
            data: {"corpId": selCorpId}
        }).done(function (data) {
            if(data){
                var content = "";
                $.each(data, function (index, item) {
                    var a = "<tr class='r-item-tr'>";
                    a += "<td style='border-left: 1px solid #DCDCDC' class='ac'><input name='ids' value='" + item.id + "' type='hidden'/><input name='maintenanceItemNames' value='"
                        + item.maintenItemName + "' type='text' data-validator='{required:true}' class='input-text' ></td>";
                    var b = "<td class='ac'><input name='remarks' value='" + (item.remark ? item.remark : "") + "' type='text' class='input-text' ></td>";
                    var d = "<td class='ac'>"
                        + "<a class='operate operate-delete' href='javascript:;'>删除</a>"
                        + "</td>"
                        + "</tr>";
                    content = content + a + b + d;
                });
                $("#r-th").after(content);
            }
        });
    };

    $right.on("click.addRowA", "#addRowA", function () { // 添加行的点击事件
        var content = "<tr class='r-item-tr'>"
            + "<td class='ac'><input name='ids' value='-1' type='hidden'/><input name='maintenanceItemNames' type='text' data-validator='{required:true}' class='input-text' ></td>"
            + "<td class='ac'><input name='remarks' type='text' class='input-text' ></td>"
            + "<td class='ac'>"
            + "<a class='operate operate-delete' href='javascript:;'>删除</a>"
            + "</td>"
            + "</tr>";
        if ($(".r-item-tr").size() > 0) {
            $(".r-item-tr").last().after(content);
        } else {
            $("#r-th").after(content);
        }
    }).on("click.delRowA", ".operate-delete", function () { // 删除行的点击事件
        $(this).closest('tr').remove();
    }).on("click.update", "#sysCodeDsc_update", function () { // 保存
        var result = $.validator($maintenanceItemForm);
        if (result) {
            $.messager.confirm("是否保存", "是否保存维保项目信息？", function (r) {
                if (r) {
                    $.ajaxExt({
                        url: "/funMaintenaSet/uptMaintenaSetInfo.htm",
                        data: $maintenanceItemForm.serializeArray()
                    }).done(function (data) {
                        initRight();
                        $.messager.alert("提示", "维保项目信息保存成功。", "info");
                    });
                }
            });
        }
    });
    initsysCorpTree();
    initHeight();

});